//
//  AppDelegate.swift
//  sinaWB
//
//  Created by 杨运旺 on 15/11/7.
//  Copyright © 2015年 杨运旺. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 测试归档的用户账户
        print(UserAccountViewModel.sharedUserAccount.userLogon)
        setupAppearance()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewController //WelcomViewController() //NewFeatureViewController()
        window?.makeKeyAndVisible()
        
        // 监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification,// 通知名称，通知中心用来识别通知的
            object: nil,                            // 发送通知的对象，如果为nil，监听任何对象
            queue: nil)                             // nil，主线程
            { [weak self ](notification) -> Void in // weak self，
            print(NSThread.currentThread())
            print(notification)
                
                let vc = notification.object != nil ? WelcomViewController() : MainViewController()
            self?.window?.rootViewController = vc // 切换控制器
        }
        
        return true
    }
    
    deinit{
        // 注销通知 - 注销指定的通知
        NSNotificationCenter.defaultCenter().removeObserver(self,// 监听者
            name: WBSwitchRootViewControllerNotification, // 监听的通知
            object: nil)                                    // 发送通知的对象
    }

   
    private func setupAppearance(){
        UINavigationBar.appearance().tintColor = WBAppearanceTintColor
        UITabBar.appearance().tintColor = WBAppearanceTintColor
    }

}

// MARK: - 界面切换代码
extension AppDelegate{
    
     /// 启动跟视图控制器
    private var defaultRootViewController:UIViewController{
        // 1.判断是否登录
        if UserAccountViewModel.sharedUserAccount.userLogon{
            return isNewVersion ? NewFeatureViewController() : WelcomViewController()
        }
        // 2.没有登录返回主控制器;
        return MainViewController()
        
    }
    
    
    /// 判断是否新版本
    private var isNewVersion: Bool{
        // 1. 当前的版本 - info.plist
        // print(NSBundle.mainBundle().infoDictionary)
        let currenVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currenVersion)!
        print("当前版本\(version)")
        
        
        // 2. `之前`的版本，把当前版本保存在用户偏好 - 如果 key 不存在，返回 0
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        print("之前版本\(sandboxVersion)")
        
        
         // 3. 保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandboxVersionKey)
        
        return version > sandboxVersion
    }
    
    
    
    
    
}
